package keye.document_manage.util;

import java.util.List;
import java.util.Map;

import keye.document_manage.entity.TableFiled;

public class SqlProviderSqlite {

	public String insert(Map<String, Object> para){
		try {
			String tableName = (String) para.get("tableName");
			String values = (String) para.get("values");
			return "INSERT INTO "+tableName+" VALUES "+values;
		} catch (org.apache.ibatis.binding.BindingException e) {
				e.printStackTrace();
				System.out.println("插入数据出现异常");
		}
		System.out.println("insertBatch Sql：");
		return "";
	}
	
	public String dropTable(Map<String, Object> para){
		String tableName = (String) para.get("tableName");
		return "DROP TABLE  "+tableName;
	}
	
	public String insertBatch(Map<String, Object> para){
		StringBuilder sb = new StringBuilder();
		try {
//			String tableName = (String) para.get("tableName");
//			@SuppressWarnings("unchecked")
//			List<Map<String, String>> values = (List<Map<String, String>>) para.get("values");
//			
//			StringBuilder sb1 = new StringBuilder(0), sb2 = new StringBuilder();
//			if(values != null && values.size()>0){
//				values.stream().forEach(m->{
//					sb2.append(" ( ");
//					m.forEach((k,v)->{
//						if(0 == sb1.length())
//							sb1.append(k+" ,");
//						sb.append(String.valueOf(v)+" ,");
//					});
//					sb2.deleteCharAt(sb1.length()-1);
//					sb2.append(" ),");
//				});
//				
//				sb1.deleteCharAt(sb1.length()-1);
//				sb2.deleteCharAt(sb1.length()-1);
//				sb.append("INSERT INTO "+tableName+" NAME ( ");
//				sb.append(sb1.toString()+" )");
//				sb.append(" VALUES "+ sb2.toString()+" ");
//			}
//			List<TableFiledValue> li = (List<TableFiledValue>) para.get("values");
//			if(li != null && li.size()>0){
//				TableFiledValue filedValue = li.get(0);
//				sb.append("INSERT INTO "+tableName+" NAME ( ");
//				li.stream().forEach(v->{
//					
//				});
//			}
		} catch (org.apache.ibatis.binding.BindingException e) {
				e.printStackTrace();
				System.out.println("插入数据出现异常");
		}
		System.out.println("insertBatch Sql："+sb);
		return sb.toString();
	}
	
	public String createTable(Map<String, Object> para){
		StringBuilder sb = new StringBuilder();
		try {
			String tableName = (String) para.get("tableName");
			@SuppressWarnings("unchecked")
			List<TableFiled> fileds = (List<TableFiled>) para.get("fileds");
			sb.append("CREATE TABLE "+tableName);
			if(fileds != null && fileds.size()>0){
				sb.append(" ( ");
				fileds.stream().forEach(filed-> {
					sb.append(filed+",");
				});
				sb.deleteCharAt(sb.length()-1);
				sb.append(" ) ");
			}
		} catch (org.apache.ibatis.binding.BindingException e) {
				e.printStackTrace();
				System.out.println("创建表出现异常");
		}
		System.out.println("createTable Sql："+sb);
		return sb.toString();
	}
}
